package admindao

import (
	"go.uber.org/zap"
	"strings"
	"wzgames/internal/admin/api/wzabind"
	"wzgames/internal/admin/model"
	"wzgames/internal/gdao"
	"wzgames/internal/response"
	"wzgames/pkg/logger"
)

type StmDao gdao.DaoSource

const sqlWzUserBetCount = "select count(*) from bet_history"

func (dao *StmDao) GetWzUserBetTotal(req wzabind.WzUserBetsReq) (int, error) {
	var c int
	var sb strings.Builder
	sb.WriteString(sqlWzUserBetCount)
	s1, args := dao.genWhere(req)
	sb.WriteString(s1)
	err := dao.Get(&c, sb.String(), args...)
	return c, err
}

const sqlWzUserBetSum = "select IFNULL(sum(`betamt`), 0) as `betamt`,IFNULL(sum(`effamt`),0) as `effamt`,IFNULL(sum(`winamt`),0) as `winamt`,IFNULL(sum(`fee`),0) as `fee` from bet_history"

func (dao *StmDao) GetWzUserBetSum(req wzabind.WzUserBetsReq) (*model.BetSum, error) {
	ret := &model.BetSum{}
	var sb strings.Builder
	sb.WriteString(sqlWzUserBetSum)
	s1, args := dao.genWhere(req)
	sb.WriteString(s1)
	err := dao.Get(ret, sb.String(), args...)
	return ret, err
}

func (dao *StmDao) genWhere(req wzabind.WzUserBetsReq) (string, []interface{}) {
	var sb strings.Builder
	var items []string
	var args []interface{}

	if req.Uid > 0 {
		items = append(items, " `uid`=? ")
		args = append(args, req.Uid)
	}
	if req.BetId > 0 {
		items = append(items, " `id`=? ")
		args = append(args, req.BetId)
	}
	if req.AgtId > 0 {
		items = append(items, " `agtid`=? ")
		args = append(args, req.AgtId)
	}
	if req.GameId > 0 {
		items = append(items, " `gameid`=? ")
		args = append(args, req.GameId)
	}
	if req.InstId > 0 {
		items = append(items, " `inst`=? ")
		args = append(args, req.InstId)
	}
	if req.RoundId > 0 {
		items = append(items, " `roundid`=? ")
		args = append(args, req.RoundId)
	}
	if req.Status >= 0 {
		items = append(items, " `status`=? ")
		args = append(args, req.Status)
	}

	if len(items) > 0 {
		sb.WriteString(" where ")
		sb.WriteString(strings.Join(items, " and "))
	}
	return sb.String(), args
}

const sqlGetUserBets = "select `id`,`uid`,`agtid`,`agtuid`,`roundid`,`currid`,`inst`,`gameid`,`uname`,`curr`,`freebet`,`win`,`status`,`created`,`updated`,`betamt`,`effamt`,`winamt`,`fee`,`payout`,`bettime`,`paytime`,`gact`,`op`,`nodeid` from bet_history"

func (dao *StmDao) GetWzUserBets(req wzabind.WzUserBetsReq, offset int) ([]*model.BetHistory, error) {
	var sb strings.Builder
	sb.WriteString(sqlGetUserBets)
	s1, args := dao.genWhere(req)
	sb.WriteString(s1)
	if req.PageSize == 0 {
		sb.WriteString(" order by bettime desc")
	} else {
		sb.WriteString(" order by bettime desc limit ?,?")
		args = append(args, offset, req.PageSize)
	}
	rows, err := dao.Queryx(sb.String(), args...)
	if err != nil {
		logger.Error("err == ", zap.String("error", err.Error()))
		return nil, err
	}
	var ret []*model.BetHistory
	for rows.Next() {
		rd := &model.BetHistory{}
		err = rows.StructScan(rd)
		if err != nil {
			logger.Error("err == ", zap.String("error", err.Error()))
			return nil, err
		}
		rd.BetAmt = response.TrimAmtZeros(rd.BetAmt)
		rd.EffAmt = response.TrimAmtZeros(rd.EffAmt)
		rd.WinAmt = response.TrimAmtZeros(rd.WinAmt)
		rd.Fee = response.TrimAmtZeros(rd.Fee)
		ret = append(ret, rd)
	}
	return ret, err
}
